package com.jxb.five;

/**
 * 搜索二维矩阵 II
 * 输入：matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5
 * 输出：true
 */
public class Search2DMatrixII_240 {

    public static void main(String[] args) {

    }

    public boolean searchMatrix(int[][] matrix, int target) {
        if (matrix == null) {
            return false;
        }

        //行和列下标的最大值
        int lineLen = matrix.length - 1;
        int rowLen = matrix[0].length - 1;

        if (target > matrix[lineLen][rowLen]) {
            return false;
        }

        //当前行和列
        int curLine = 0;
        int curRow = rowLen;

        while (curLine <= lineLen && curRow >= 0) {
            if (target == matrix[curLine][curRow]) {
                return true;
            }else if (target > matrix[curLine][curRow]) {
                curLine ++;
            } else if (target < matrix[curLine][curRow]) {
                curRow --;
            }
        }
        return false;
    }

}
